package com.rachel.studyapp.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.rachel.studyapp.R;
import com.rachel.studyapp.entries.UserInfo;
import com.rachel.studyapp.mvp.ContractUtils;
import com.rachel.studyapp.mvp.present.MinePresent;

import de.greenrobot.event.EventBus;

public class RegisterActivity extends BaseActivity implements View.OnClickListener,
        ContractUtils.IMineView{
    private static final String TAG = "zsr";

    private ImageView mLeftIcon;
    private TextView mToolbarText;
    private MinePresent mPresent;
    private ProgressDialog mProgressDialog;
    /**
     * 手机/邮箱/昵称
     */
    private EditText mRegisterUsername;
    /**
     * 请输入密码
     */
    private EditText mRegisterPassword;
    /**
     * 请再次输入密码
     */
    private EditText mRegisterPasswordCheck;
    private Button mRegisterRegister;
    private String mUsername;
    private String mPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        mPresent = new MinePresent(this,this);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    private void initView() {
        mLeftIcon = (ImageView) findViewById(R.id.left_icon);
        mLeftIcon.setOnClickListener(this);
        mToolbarText = (TextView) findViewById(R.id.toolbar_text);
        mRegisterUsername = (EditText) findViewById(R.id.register_username);
        mRegisterPassword = (EditText) findViewById(R.id.register_password);
        mRegisterPasswordCheck = (EditText) findViewById(R.id.register_password_check);
        mRegisterRegister = (Button) findViewById(R.id.register_register);
        mRegisterRegister.setOnClickListener(this);

        mLeftIcon.setVisibility(View.VISIBLE);
        mLeftIcon.setImageResource(R.mipmap.back);
        mLeftIcon.setOnClickListener(this);
        mToolbarText.setVisibility(View.VISIBLE);
        mToolbarText.setText("用户注册");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_icon:
                finish();
                break;
            case R.id.register_register:
                mUsername = mRegisterUsername.getText().toString();
                mPassword = mRegisterPassword.getText().toString();
                String passwordcheck = mRegisterPasswordCheck.getText().toString();
                if (mPassword.equals(passwordcheck)){
                    mPresent.register(new UserInfo(mUsername, mPassword));
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getWindow().peekDecorView().getWindowToken(),0);
                }else{
                    Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }


    @Override
    public void loginSuccess(UserInfo info) {

    }

    @Override
    public void loginFailure(Object errorObj) {

    }

    @Override
    public void registerSuccess(UserInfo info) {
        EventBus.getDefault().post(info);
        Toast.makeText(this, "注册成功，请登陆", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void registerFailure(Object errorObj) {
        Toast.makeText(this, errorObj.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showloading() {
        Log.d(TAG, "showloading: ");
        mProgressDialog = ProgressDialog.show(RegisterActivity.this,null,"正在注册，请稍后");
        mProgressDialog.setCancelable(true);
    }

    @Override
    public void hideloading() {
        Log.d(TAG, "hideloading: ");
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void onFailure(Object errorObj) {

    }
}
